// miniprogram/pages/redeem/history.js
Page({
  data: {
    redeemHistory: [],
    filteredHistory: [],
    filterType: 'all' // all, completed, expired
  },

  onLoad() {
    this.loadRedeemHistory()
  },

  loadRedeemHistory() {
    // 模拟兑换记录数据
    const redeemHistory = [
      {
        id: 1,
        itemName: '星巴克咖啡券',
        points: 50,
        couponCode: 'ST12345678',
        status: 'completed', // completed, expired
        redeemTime: '2023-05-10 15:30',
        expireDate: '2023-06-10',
        image: '/images/redeem_starbucks.png'
      },
      {
        id: 2,
        itemName: '电影票',
        points: 80,
        couponCode: 'MV87654321',
        status: 'completed',
        redeemTime: '2023-05-05 10:15',
        expireDate: '2023-06-05',
        image: '/images/redeem_movie.png'
      },
      {
        id: 3,
        itemName: '10元话费充值',
        points: 100,
        couponCode: 'PH13579246',
        status: 'expired',
        redeemTime: '2023-04-01 09:20',
        expireDate: '2023-05-01',
        image: '/images/redeem_phone.png'
      }
    ]
    
    this.setData({ 
      redeemHistory,
      filteredHistory: redeemHistory
    })
  },

  filterHistory(e) {
    const type = e.currentTarget.dataset.type
    let filteredHistory = []
    
    if (type === 'all') {
      filteredHistory = this.data.redeemHistory
    } else {
      filteredHistory = this.data.redeemHistory.filter(item => item.status === type)
    }
    
    this.setData({ 
      filterType: type,
      filteredHistory
    })
  },

  copyCouponCode(e) {
    const couponCode = e.currentTarget.dataset.code
    wx.setClipboardData({
      data: couponCode,
      success: () => {
        wx.showToast({
          title: '券码已复制',
          icon: 'success'
        })
      }
    })
  }
})